<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var deepestLeavesSum = function (root) {
      let arr = []
      function dfs(node, depth) {
        if (!node) return 0
        arr[depth] = arr[depth] || []
        arr[depth].push(node.val)
        dfs(node.left, depth + 1)
        dfs(node.right, depth + 1)
      }
      dfs(root, 0)
      return arr.pop().reduce((sum, cur) => sum + cur)
    };
    console.log(deepestLeavesSum(root = [6, 7, 8, 2, 7, 1, 3, 9, null, 1, 4, null, null, null, 5]))
  </script>
</body>

</html>